/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin.module.uno;

import java.util.HashMap;
import java.util.Map;

import sh.grapendaal.tsuushin.annotation.Callback;
import sh.grapendaal.tsuushin.annotation.Module;
import sh.grapendaal.tsuushin.core.Context;
import sh.grapendaal.tsuushin.message.IncomingMessage;
import sh.grapendaal.tsuushin.message.MessageType;
import sh.grapendaal.tsuushin.message.PrivateMessage;
import sh.grapendaal.tsuushin.module.uno.engine.CommunicationPipeline;
import sh.grapendaal.tsuushin.module.uno.engine.Player;
import sh.grapendaal.tsuushin.module.uno.engine.UnoEngine;
import sh.grapendaal.tsuushin.module.uno.engine.UnoException;

/**
 * Uno IRC module.
 */
@Module(author = "Dik Grapendaal", version = "0.0.1")
public class Uno {

	/**
	 * This keeps track of the running Uno games. The module supports running
	 * multiple Uno games at the same time, one per channel.
	 */
	private Map<String, UnoEngine> games = new HashMap<String, UnoEngine>();

	@Callback(type = MessageType.CHANNEL_MESSAGE)
	public void onPrivateMessage(Context context, IncomingMessage message) {
		PrivateMessage incoming = (PrivateMessage) message;

		// TODO Better command handling.
		if (incoming.getContent().startsWith("jo")) {
			this.joinGame(context, incoming);
		} else if (incoming.getContent().startsWith("le")) {
			this.leaveGame(context, incoming);
		} else if (incoming.getContent().startsWith("st")) {
			this.startGame(context, incoming);
		}
	}

	private void joinGame(Context context, PrivateMessage incoming) {
		String channel = incoming.getDestination().toLowerCase();
		if (!games.containsKey(channel)) {
			UnoEngine engine = new UnoEngine();
			CommunicationPipeline pipeline = new CommunicationPipeline(context.getBot(), engine);
			engine.setCommunication(pipeline);
			
			games.put(channel, engine);
		}

		games.get(channel).addPlayer(
				new Player(incoming.getUser().getNickname()));

		PrivateMessage outgoing = new PrivateMessage();
		outgoing.setDestination(incoming.getDestination());
		outgoing.setContent(incoming.getUser().getNickname()
				+ " has joined the game.");
		context.getBot().send(outgoing);
	}

	private void leaveGame(Context context, PrivateMessage incoming) {
		String channel = incoming.getDestination().toLowerCase();
		if (!games.containsKey(channel)) {
			return;
		}

		games.get(channel).removePlayer(
				new Player(incoming.getUser().getNickname()));

		PrivateMessage outgoing = new PrivateMessage();
		outgoing.setDestination(incoming.getDestination());
		outgoing.setContent(incoming.getUser().getNickname() + " has left the game.");
		context.getBot().send(outgoing);
	}
	
	private void startGame(Context context, PrivateMessage incoming) {
		String channel = incoming.getDestination().toLowerCase();
		if (!games.containsKey(channel)) {
			return;
		}
		
		PrivateMessage outgoing = new PrivateMessage();
		outgoing.setDestination(incoming.getDestination());
		
		try {
			games.get(channel).start();
			outgoing.setContent("Uno has been started!");
		} catch (UnoException e) {
			outgoing.setContent(e.getMessage());
		}
		
		context.getBot().send(outgoing);
	}

}
